package com.ete.service.callback.version

import com.blankj.utilcode.util.ToastUtils
import com.ete.service.dialog.version.CheckVersionDialog
import com.ete.service.entry.version.VersionUpgradeInfo
import com.ete.service.manager.log.LogManager
import com.ete.service.service.R
import com.ete.service.service.version.VersionHelper
import com.ete.service.util.log.EteLog

/**
 * Author:LiXian
 * <p>
 * Desc:手动检测
 *
 * 注：用于手动检测，是否有新版本，有加载框，返回是否有新版本
 * Version:1.0
 * date 2023/12/21 14:08
 */
abstract class ManualCheckCallback : IVersionCallback {

    private var mCheckDialog: CheckVersionDialog? = null

    override fun onStart() {
        EteLog.d(LogManager.TAG, "手动检查版本，开始获取最新版本信息")
        mCheckDialog = VersionHelper.showCheckVersionDialog()
        mCheckDialog?.show()
    }

    override fun onError(errorMsg: String) {
        EteLog.d(LogManager.TAG, "手动检查版本，获取最新版本信息错误 errorMsg:[${errorMsg}]")
        mCheckDialog?.dismiss()
        mCheckDialog = null
        ToastUtils.showShort(errorMsg)
        isNewVersion(false)
    }

    override fun onNoNewVersion() {
        EteLog.d(LogManager.TAG, "手动检查版本，无最新版本信息")
        mCheckDialog?.dismiss()
        mCheckDialog = null
        ToastUtils.showShort(R.string.version_last_version)
        isNewVersion(false)
    }

    override fun onVersionInfo(mVersion: VersionUpgradeInfo) {
        mCheckDialog?.dismiss()
        mCheckDialog = null
        EteLog.d(LogManager.TAG, "手动检查版本，获取到最新版本信息mVersion:[${mVersion}]")
        val isUpgradeVersion = VersionHelper.isHadNewVersion(mVersion)
        EteLog.d(LogManager.TAG, "手动检查版本，是否有新版本isUpgradeVersion:[$isUpgradeVersion]")
        if (isUpgradeVersion) {
            VersionHelper.showUpgradeDialog(mVersion, mCancelCallback = {
                cancelUpgrade()
            })
        }
        isNewVersion(isUpgradeVersion)
    }

    abstract fun isNewVersion(isNewVersion: Boolean)

    open fun cancelUpgrade() {

    }
}